package com.hnas.trade.service.mapper;

import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.BeanUtils;

import com.hnas.core.mapper.Mapper;
import com.hnas.core.util.StringUtil;
import com.hnas.trade.domain.OrderStatus;
import com.hnas.trade.domain.PayStatus;
import com.hnas.trade.orm.Order;
import com.hnas.trade.service.dto.DTOOrder;
import com.hnas.trade.service.dto.DTOTradeRecord;

public class OrderMapper extends Mapper<DTOOrder, Order>{
	private static OrderMapper mapper;
	private OrderMapper(){
		super(DTOOrder.class,Order.class);
	}
	public static OrderMapper getInstance(){
		if(mapper == null){
			mapper = new OrderMapper();
		}
		return mapper;
	}
	public DTOOrder mapToValue(Order vo){
		if(vo == null) return null;
		DTOOrder dto = new DTOOrder();
		BeanUtils.copyProperties(vo, dto);
		if(StringUtil.isNotEmpty(dto.getOrderStatus())){
			dto.setOrderStatusName(OrderStatus.getOrderStatusString(dto.getOrderStatus()));
		}
		if(StringUtil.isNotEmpty(dto.getPayStatus())){
			dto.setPayStatusName(PayStatus.getPayStatusString(dto.getPayStatus()));
		}
		try {
			if(null != dto.getNeedPay()){
				dto.setNeedPayDouble((double)dto.getNeedPay() / 100);
			}
			if(null != dto.getRealPay()){
				dto.setRealPayDouble(((double)dto.getRealPay()) / 100);
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return dto;
	}
	public List<DTOOrder> mapToValue(List<Order> list){
		if(list == null) return null;
		List<DTOOrder> dtoList = new ArrayList<DTOOrder>();
		for(Order vo : list){
			dtoList.add(mapToValue(vo));
		}
		return dtoList;
	}
	
	public DTOTradeRecord mapToDTOTradeRecord(Order vo){
		if(vo == null) return null;
		DTOTradeRecord dto = new DTOTradeRecord();
		BeanUtils.copyProperties(vo, dto);
		return dto;
	}
	
}
